package com.yanqu.road.server.manger.activity.fanxiuspring;

import com.yanqu.road.dao.impl.activity.fanxiuspring.FanXiuSpringConfigDaoImpl;
import com.yanqu.road.entity.activity.ActivityConfig;
import com.yanqu.road.entity.activity.ActivityInfo;
import com.yanqu.road.entity.activity.fanxiuspring.FanXiuSpringConfig;
import com.yanqu.road.entity.enums.eActivityConfigType;
import com.yanqu.road.entity.enums.eActivityType;
import com.yanqu.road.server.TempMgr;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.activity.fanxiuspring.FanXiuSpringModule;
import com.yanqu.road.server.manger.activity.NormalActivityMgr;
import com.yanqu.road.server.manger.player.GamePlayerMgr;
import com.yanqu.road.utils.date.DateHelper;
import com.yanqu.road.utils.string.StringUtils;

import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class FanXiuSpringMgr extends TempMgr {

    public static final int CHILD_TYPE_SIGN = 1;

    public static final int CHILD_TYPE_OTHER = 2;

    private static Map<Integer, FanXiuSpringConfig> configMap = new ConcurrentHashMap<>();

    @Override
    public boolean init() throws Exception {
        return reload();
    }

    @Override
    public boolean reloadConfig() throws Exception {
        return true;
    }

    @Override
    public boolean reloadData() throws Exception {
        return true;
    }

    @Override
    public boolean stop() {
        return false;
    }

    public static void reloadActivityData() {
        getLogger().info("reload FanXiu Spring Activity start");

        List<ActivityInfo> openActivityInfoList = NormalActivityMgr.getOpenActivityInfoList(eActivityType.FanXiuSpringActivity.getValue());
        if (openActivityInfoList.isEmpty()) {
            getLogger().info("no FanXiu Spring Activity in show time");
            configMap = new ConcurrentHashMap<>();
        } else {

            Map<Integer, FanXiuSpringConfig> tempMap = new ConcurrentHashMap<>();

            for (ActivityInfo activityInfo : openActivityInfoList) {

                int tmpActivityId = activityInfo.getActivityId();

                FanXiuSpringConfig configTemp = new FanXiuSpringConfig();
                configTemp.setActivityInfo(activityInfo);

                //初始化配置
                Map<String, ActivityConfig> activityConfigMap = NormalActivityMgr.getActivityConfigMap(tmpActivityId);
                if(activityInfo.getChildType() == CHILD_TYPE_OTHER) {
                    configTemp.setDrawInfoList(new FanXiuSpringConfigDaoImpl().getFanXiuSpringDrawInfoList(tmpActivityId));

                    if (activityConfigMap.containsKey(eActivityConfigType.XIUXIAN_NEWYEAR_EXCHANGE_SCORE_ITEM_ID.getName())) {
                        configTemp.setScoreItemId(activityConfigMap.get(eActivityConfigType.XIUXIAN_NEWYEAR_EXCHANGE_SCORE_ITEM_ID.getName()).getIntValue());
                    }else if(activityConfigMap.containsKey(eActivityConfigType.EXCHANGE_SCORE_ITEM_ID.getName())){
                        configTemp.setScoreItemId(activityConfigMap.get(eActivityConfigType.EXCHANGE_SCORE_ITEM_ID.getName()).getIntValue());
                    }
                    if (activityConfigMap.containsKey(eActivityConfigType.XIUXIAN_NEWYEAR_PART2_PICE_ITEM_ID.getName())) {
                        configTemp.setPiceItemId(activityConfigMap.get(eActivityConfigType.XIUXIAN_NEWYEAR_PART2_PICE_ITEM_ID.getName()).getIntValue());
                    }
                    if (activityConfigMap.containsKey(eActivityConfigType.XIUXIAN_NEW_DRAW_OPP_ADD.getName())) {
                        configTemp.setDrawOppAdd(activityConfigMap.get(eActivityConfigType.XIUXIAN_NEW_DRAW_OPP_ADD.getName()).getIntValue());
                    }
                    if (activityConfigMap.containsKey(eActivityConfigType.XIUXIAN_NEW_DRAW_OPP_MAX.getName())) {
                        configTemp.setDrawOppMax(activityConfigMap.get(eActivityConfigType.XIUXIAN_NEW_DRAW_OPP_MAX.getName()).getIntValue());
                    }
                    if (activityConfigMap.containsKey(eActivityConfigType.XIUXIAN_NEW_DRAW_RARE_POOL.getName())) {
                        configTemp.setDrawRarePool(activityConfigMap.get(eActivityConfigType.XIUXIAN_NEW_DRAW_RARE_POOL.getName()).getValue());
                    }
                    if (activityConfigMap.containsKey(eActivityConfigType.XIUXIAN_NEW_DRAW_SCORE_NUM.getName())) {
                        configTemp.setDrawScoreGet(activityConfigMap.get(eActivityConfigType.XIUXIAN_NEW_DRAW_SCORE_NUM.getName()).getIntValue());
                    }
                    if (activityConfigMap.containsKey(eActivityConfigType.XIUXIAN_NEW_DRAW_TEN.getName())) {
                        configTemp.setTenDrawScoreLimit(activityConfigMap.get(eActivityConfigType.XIUXIAN_NEW_DRAW_TEN.getName()).getIntValue());
                    }
                    if (activityConfigMap.containsKey(eActivityConfigType.XIUXIAN_NEW_DRAW_NOTICE.getName())) {
                        configTemp.setDrawNoticeCount(activityConfigMap.get(eActivityConfigType.XIUXIAN_NEW_DRAW_NOTICE.getName()).getIntValue());
                    }
                    if (activityConfigMap.containsKey(eActivityConfigType.XIUXIAN_NEW_DRAW_RARE_WEIGHT_PARAM.getName())) {
                        configTemp.setDrawRareWeight(activityConfigMap.get(eActivityConfigType.XIUXIAN_NEW_DRAW_RARE_WEIGHT_PARAM.getName()).getValue());
                    }
                    if (activityConfigMap.containsKey(eActivityConfigType.XIUXIAN_NEW_EXTRACT_ITEM_RECYCLE.getName())){
                        configTemp.setItemRecycle(activityConfigMap.get(eActivityConfigType.XIUXIAN_NEW_EXTRACT_ITEM_RECYCLE.getName()).getValue());
                    }
                    if (activityConfigMap.containsKey("XIUXIAN_NEWYEAR_PART2_PIC_SIZE")){
                        List<Integer> list = StringUtils.stringToIntegerList(activityConfigMap.get("XIUXIAN_NEWYEAR_PART2_PIC_SIZE").getValue(), "\\|");
                        if(list.size() == 2){
                            configTemp.setPIC_SIZE(list.get(0) * list.get(1));
                        }
                    }
                }else {
                    if (activityConfigMap.containsKey(eActivityConfigType.XIUXIAN_NEW_PART1_FIX_COST.getName())) {
                        configTemp.setXIUXIAN_NEW_PART1_FIX_COST(StringUtils.stringToStringList(activityConfigMap.get(eActivityConfigType.XIUXIAN_NEW_PART1_FIX_COST.getName()).getValue(), "\\|"));
                    }

                }
                configTemp.afterInit();

                tempMap.put(tmpActivityId, configTemp);
            }

            configMap = tempMap;
        }

        getLogger().info("reload FanXiu Spring Activity finish");

        // 在线玩家下发数据
        for (GamePlayer gamePlayer : GamePlayerMgr.getAllOnlinePlayer()) {
            FanXiuSpringModule module = gamePlayer.getModule(FanXiuSpringModule.class);
            module.syncConfig();
            module.initUserData();
            module.syncUserData();
        }
    }


    public static ActivityInfo getActivityInfo(int childType) {
        for (Map.Entry<Integer, FanXiuSpringConfig> entry : configMap.entrySet()) {
            if(entry.getValue().getActivityInfo().getChildType() == childType){
                return entry.getValue().getActivityInfo();
            }
        }
        return null;
    }

    public static FanXiuSpringConfig getActivityConfig(int childType) {
        for (Map.Entry<Integer, FanXiuSpringConfig> entry : configMap.entrySet()) {
            if(entry.getValue().getActivityInfo().getChildType() == childType){
                return entry.getValue();
            }
        }
        return null;
    }


    public static int getActivityId(int childType){
        FanXiuSpringConfig config = getActivityConfig(childType);
        if(config == null){
            return 0;
        }
        return config.getActivityInfo().getActivityId();
    }


    public static long getDiffDayNum(long time) {
        long todayZeroTime = DateHelper.getTodayZeroTimeStamp() / 1000;
        long zeroTime = DateHelper.getZeroTimeStamp(time) / 1000;
        if (todayZeroTime >= zeroTime) {
            return (todayZeroTime - zeroTime) / DateHelper.DAY_SECONDS + 1;
        }
        return (zeroTime - todayZeroTime) / DateHelper.DAY_SECONDS + 1;
    }

    public static Map<Integer, FanXiuSpringConfig> getConfigMap() {
        return configMap;
    }
}
